package com.yunhe.rentcars.pro.service.permi.impl;


import com.yunhe.rentcars.commons.annoaction.Log;
import com.yunhe.rentcars.commons.util.StringUtils;
import com.yunhe.rentcars.pro.dao.permi.PermissionDao;
import com.yunhe.rentcars.pro.entity.permi.Permission;
import com.yunhe.rentcars.commons.dto.Pageable;
import com.yunhe.rentcars.commons.dto.TreeNode;
import com.yunhe.rentcars.pro.service.permi.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/10 0010.
 */
@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;

    @Log(value = "添加权限(t_resource)",type = 1)

    @Override
    public void save(Permission permission) {
        permissionDao.save(permission);
    }


    @Override
    public void delete(Long id) {
        permissionDao.delete(id);
    }

    @Log(value = "删除权限(t_resource)",type = 2)
    @Override
    public void deleteBatch(String ids) {
        if(StringUtils.notEmpty(ids)){
            String[] idArr = ids.split(",");
            for(String i:idArr){
                this.delete(new Long(i));
            }
        }
    }

    @Log(value = "修改权限(t_resource)",type = 3)
    @Override
    public void update(Permission permission) {

        permissionDao.update(permission);
    }

    @Override
    public Permission find(Long id) {

        Permission permission = permissionDao.find(id);
        return permission;
    }

    @Override
    public List<Permission> findList() {

        List<Permission> permissionList = permissionDao.findList();
        return permissionList;
    }

    @Override
    public Pageable<Permission> findPage(Integer offset, Integer limit) {

        List<Permission> permissionList = permissionDao.findPage(offset, limit);
        int count = this.count();
        Pageable<Permission> pageable = new Pageable();
        pageable.setTotal(count);
        pageable.setRows(permissionList);
        return pageable;
    }

    @Override
    public int count() {

        int count = permissionDao.count();
        return count;
    }

    @Override
    public List<Permission> findPermisListByDan(Permission permission) {

        List<Permission> permissionList = permissionDao.findPermisListByDan(permission);
        return permissionList;
    }

    @Override
    public List<Permission> findPermisListByDuo(Map map) {

        List<Permission> permissionList = permissionDao.findPermisListByDuo(map);
        return permissionList;
    }

    @Override
    public List<TreeNode> findTreeNodes(Long roleid) {
        List<TreeNode> treeNodes = new ArrayList<>();
        List<Permission> allPermission = this.findList();

        List rpIdList = new ArrayList();

        if(null != roleid){
            Map map = new HashMap();
            map.put("roleid",roleid);
            List<Permission> rolePermission = this.findPermisListByDuo(map);
            if(null != rolePermission){
                for(Permission p : rolePermission){
                    rpIdList.add(p.getPermisid());
                }
            }
        }


        if(null != allPermission){
            for(Permission p : allPermission){
                TreeNode treeNode = new TreeNode(p.getPermisid(),p.getParentid(),p.getPermisname());
                if(null != rpIdList && rpIdList.contains(p.getPermisid())){
                    treeNode.setChecked(Boolean.TRUE);
                }
                treeNodes.add(treeNode);
            }
        }

        return treeNodes;
    }
}
